package com.sc.design.mediator;

public class Mediator extends AbstractMediator {
    @Override
    public void execute(String str, Object... objects) {
        if(str.equalsIgnoreCase("purchase.buy")){
            this.buyComputer((Integer)objects[0]);
        }else  if(str.equalsIgnoreCase("sale.sell")){
            this.sellComputer((Integer) objects[0]);
        }else if(str.equalsIgnoreCase("sale.offsell")){
            this.offSell();
        }else if(str.equalsIgnoreCase("stock.clear")){
            this.clearStock();
        }
    }

    private void buyComputer(int count){
        int saleStatus = super.sale.getSaleStatus();
        if(saleStatus>80){
            System.out.println("采购"+count);
            super.stock.increase(count);
        }else{
            int buyNum = count/2;
            System.out.println("采购"+buyNum);
            super.stock.increase(buyNum);
        }
    }
    private void sellComputer(int num){
        if(super.stock.getStock()<num){
            super.purchase.buyComputer(num);
        }
        super.stock.decreate(num);
    }

    private void offSell(){
        System.out.println("折价出售"+stock.getStock());
    }

    private void clearStock(){
        super.sale.offSale();
        super.purchase.refuseBuy();
    }
}
